package net.hands.common.web.html;
/**
 * 说明:html编码器
 * <br>
 * <p>这是一个开源的软件，请在LGPLv3下合法使用、修改或重新发布。</p>
 * @author 陈侃(cknet@126.com, 13910090885)
 *         http://code.google.com/p/simpleframework/
 *         http://www.simpleframework.net
 */
public class HtmlEncoder {
   /**
    * 内容
    */
	protected static final char[][] TEXT = new char[64][];
	/**
	 * 大字段
	 */
	protected static final char[][] BLOCK = new char[64][];
	/**
	 * 空格
	 */
	private static final String NBSP = "&nbsp;";

	static {
		for (int i = 0; i < 64; i++) {
			TEXT[i] = new char[] { (char) i };
		}

		// special HTML characters
		TEXT['\''] = "&#039;".toCharArray();

		TEXT['"'] = "&quot;".toCharArray();
		TEXT['&'] = "&amp;".toCharArray();
		TEXT['<'] = "&lt;".toCharArray();
		TEXT['>'] = "&gt;".toCharArray();

		// text table
		System.arraycopy(TEXT, 0, BLOCK, 0, 64);
		BLOCK['\n'] = "<br/>".toCharArray();
		BLOCK['\r'] = "<br/>".toCharArray();
	}
   /**
   * 说明:内容编码
   * @param text 内容
   * @return String
   * @update 2012-9-20 下午3:58:05
    */
	public static String text(final String text) {
		int len;
		if ((text == null) || ((len = text.length()) == 0)) {
			return "";
		}
		final StringBuilder buffer = new StringBuilder(len + (len >> 2));
		for (int i = 0; i < len; i++) {
			final char c = text.charAt(i);
			if (c < 64) {
				buffer.append(TEXT[c]);
			} else {
				buffer.append(c);
			}
		}
		return buffer.toString();
	}
	/**
	*说明:大字段编码 
	* @param text 内容
	* @return String
	* @update 2012-9-20 下午3:58:36
	 */

	public static String block(final String text) {
		int len;
		if ((text == null) || ((len = text.length()) == 0)) {
			return "";
		}
		final StringBuilder buffer = new StringBuilder(len + (len >> 2));
		char c, prev = 0;
		for (int i = 0; i < len; i++, prev = c) {
			c = text.charAt(i);
			if ((c == '\n') && (prev == '\r')) {
				continue; // previously '\r' (CR) was encoded, so skip '\n' (LF)
			}
			if (c < 64) {
				buffer.append(BLOCK[c]);
			} else {
				buffer.append(c);
			}
		}
		return buffer.toString();
	}
   /**
   * 说明:编码
   * @param text 内容
   * @return String
   * @update 2012-9-20 下午3:59:01
    */
	public static String strict(final String text) {
		int len;
		if ((text == null) || ((len = text.length()) == 0)) {
			return "";
		}
		final StringBuilder buffer = new StringBuilder(len + (len >> 2));
		char c, prev = 0;
		boolean prevSpace = false;
		for (int i = 0; i < len; i++, prev = c) {
			c = text.charAt(i);
			if (c == ' ') {
				if (prev != ' ') {
					prevSpace = false;
				}
				if (prevSpace == false) {
					buffer.append(' ');
				} else {
					buffer.append(NBSP);
				}
				prevSpace = !prevSpace;
				continue;
			}
			if ((c == '\n') && (prev == '\r')) {
				continue; // previously '\r' (CR) was encoded, so skip '\n' (LF)
			}
			if (c < 64) {
				buffer.append(BLOCK[c]);
			} else {
				buffer.append(c);
			}
		}
		return buffer.toString();
	}
}
